home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / Average.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-04-25  |  6.7 KB  |  212 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmAverage 
  4.    Caption         =   "Average []"
  5.    ClientHeight    =   4800
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   8400
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   4800
  11.    ScaleWidth      =   8400
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.PictureBox picResult 
  14.       Height          =   2265
  15.       Left            =   5640
  16.       ScaleHeight     =   147
  17.       ScaleMode       =   3  'Pixel
  18.       ScaleWidth      =   173
  19.       TabIndex        =   0
  20.       Top             =   1200
  21.       Width           =   2655
  22.    End
  23.    Begin VB.PictureBox picOriginal 
  24.       AutoSize        =   -1  'True
  25.       Height          =   2265
  26.       Index           =   3
  27.       Left            =   2880
  28.       Picture         =   "Average.frx":0000
  29.       ScaleHeight     =   147
  30.       ScaleMode       =   3  'Pixel
  31.       ScaleWidth      =   173
  32.       TabIndex        =   4
  33.       Top             =   2400
  34.       Width           =   2655
  35.    End
  36.    Begin VB.PictureBox picOriginal 
  37.       AutoSize        =   -1  'True
  38.       Height          =   2265
  39.       Index           =   2
  40.       Left            =   120
  41.       Picture         =   "Average.frx":12ADA
  42.       ScaleHeight     =   147
  43.       ScaleMode       =   3  'Pixel
  44.       ScaleWidth      =   173
  45.       TabIndex        =   3
  46.       Top             =   2400
  47.       Width           =   2655
  48.    End
  49.    Begin VB.PictureBox picOriginal 
  50.       AutoSize        =   -1  'True
  51.       Height          =   2265
  52.       Index           =   1
  53.       Left            =   2880
  54.       Picture         =   "Average.frx":255B4
  55.       ScaleHeight     =   147
  56.       ScaleMode       =   3  'Pixel
  57.       ScaleWidth      =   173
  58.       TabIndex        =   2
  59.       Top             =   0
  60.       Width           =   2655
  61.    End
  62.    Begin MSComDlg.CommonDialog dlgOpenFile 
  63.       Left            =   0
  64.       Top             =   840
  65.       _ExtentX        =   847
  66.       _ExtentY        =   847
  67.       _Version        =   393216
  68.    End
  69.    Begin VB.PictureBox picOriginal 
  70.       AutoSize        =   -1  'True
  71.       Height          =   2265
  72.       Index           =   0
  73.       Left            =   120
  74.       Picture         =   "Average.frx":3808E
  75.       ScaleHeight     =   147
  76.       ScaleMode       =   3  'Pixel
  77.       ScaleWidth      =   173
  78.       TabIndex        =   1
  79.       Top             =   0
  80.       Width           =   2655
  81.    End
  82.    Begin VB.Menu mnuFile 
  83.       Caption         =   "&File"
  84.       Begin VB.Menu mnuFileSaveAs 
  85.          Caption         =   "Save &As..."
  86.          Shortcut        =   ^A
  87.       End
  88.    End
  89. Attribute VB_Name = "frmAverage"
  90. Attribute VB_GlobalNameSpace = False
  91. Attribute VB_Creatable = False
  92. Attribute VB_PredeclaredId = True
  93. Attribute VB_Exposed = False
  94. Option Explicit
  95. ' Transform the image.
  96. Private Sub TransformImage()
  97. Dim pixels0() As RGBTriplet
  98. Dim pixels1() As RGBTriplet
  99. Dim pixels2() As RGBTriplet
  100. Dim pixels3() As RGBTriplet
  101. Dim new_pixels() As RGBTriplet
  102. Dim bits_per_pixel As Integer
  103. Dim r As Integer
  104. Dim g As Integer
  105. Dim b As Integer
  106. Dim X As Integer
  107. Dim Y As Integer
  108.     ' Get the pixels from picOriginal images.
  109.     GetBitmapPixels picOriginal(0), pixels0, bits_per_pixel
  110.     GetBitmapPixels picOriginal(1), pixels1, bits_per_pixel
  111.     GetBitmapPixels picOriginal(2), pixels2, bits_per_pixel
  112.     GetBitmapPixels picOriginal(3), pixels3, bits_per_pixel
  113.     ' Allocate the new_pixels array.
  114.     ReDim new_pixels( _
  115.         LBound(pixels0, 1) To UBound(pixels0, 1), _
  116.         LBound(pixels0, 2) To UBound(pixels0, 2))
  117.     ' Set the pixel color values.
  118.     For Y = 1 To picOriginal(0).ScaleHeight - 2
  119.         For X = 1 To picOriginal(0).ScaleWidth - 2
  120.             r = 0
  121.             g = 0
  122.             b = 0
  123.             With pixels0(X, Y)
  124.                 r = r + .rgbRed
  125.                 g = g + .rgbGreen
  126.                 b = b + .rgbBlue
  127.             End With
  128.             With pixels1(X, Y)
  129.                 r = r + .rgbRed
  130.                 g = g + .rgbGreen
  131.                 b = b + .rgbBlue
  132.             End With
  133.             With pixels2(X, Y)
  134.                 r = r + .rgbRed
  135.                 g = g + .rgbGreen
  136.                 b = b + .rgbBlue
  137.             End With
  138.             With pixels3(X, Y)
  139.                 r = r + .rgbRed
  140.                 g = g + .rgbGreen
  141.                 b = b + .rgbBlue
  142.             End With
  143.             With new_pixels(X, Y)
  144.                 .rgbRed = r / 4
  145.                 .rgbGreen = g / 4
  146.                 .rgbBlue = b / 4
  147.             End With
  148.         Next X
  149.     Next Y
  150.     ' Set picResult's pixels.
  151.     SetBitmapPixels picResult, bits_per_pixel, new_pixels
  152.     picResult.Picture = picResult.Image
  153. End Sub
  154. ' Start in the current directory.
  155. Private Sub Form_Load()
  156. Dim i As Integer
  157.     For i = 0 To 3
  158.         picOriginal(i).AutoSize = True
  159.         picOriginal(i).ScaleMode = vbPixels
  160.         picOriginal(i).AutoRedraw = True
  161.     Next i
  162.     picResult.ScaleMode = vbPixels
  163.     picResult.AutoRedraw = True
  164.     dlgOpenFile.CancelError = True
  165.     dlgOpenFile.InitDir = App.Path
  166.     dlgOpenFile.Filter = _
  167.         "Bitmaps (*.bmp)|*.bmp|" & _
  168.         "GIFs (*.gif)|*.gif|" & _
  169.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  170.         "Icons (*.ico)|*.ico|" & _
  171.         "Cursors (*.cur)|*.cur|" & _
  172.         "Run-Length Encoded (*.rle)|*.rle|" & _
  173.         "Metafiles (*.wmf)|*.wmf|" & _
  174.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  175.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  176.         "All Files (*.*)|*.*"
  177.     TransformImage
  178. End Sub
  179. ' Save the transformed image.
  180. Private Sub mnuFileSaveAs_Click()
  181. Dim file_name As String
  182.     ' Let the user select a file.
  183.     On Error Resume Next
  184.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  185.     dlgOpenFile.ShowSave
  186.     If Err.Number = cdlCancel Then
  187.         Exit Sub
  188.     ElseIf Err.Number <> 0 Then
  189.         Beep
  190.         MsgBox "Error selecting file.", , vbExclamation
  191.         Exit Sub
  192.     End If
  193.     On Error GoTo 0
  194.     Screen.MousePointer = vbHourglass
  195.     DoEvents
  196.     file_name = Trim$(dlgOpenFile.FileName)
  197.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  198.         - Len(dlgOpenFile.FileTitle) - 1)
  199.     Caption = "Average [" & dlgOpenFile.FileTitle & "]"
  200.     ' Save the transformed image into the file.
  201.     On Error GoTo SaveError
  202.     SavePicture picResult.Picture, file_name
  203.     On Error GoTo 0
  204.     Screen.MousePointer = vbDefault
  205.     Exit Sub
  206. SaveError:
  207.     Screen.MousePointer = vbDefault
  208.     MsgBox "Error " & Format$(Err.Number) & _
  209.         " saving file '" & file_name & "'" & vbCrLf & _
  210.         Err.Description
  211. End Sub
  212.